package com.bestv.lab;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;


public class ABTestHasher {

	private String queryString = "select distinct userid from accesslog_daily";
	private String updateString = "update accesslog_daily set abgroup=? where userid=?";
	
	@Test
	public void insertABGroup() {
		Connection conn = null;
		ResultSet rs = null;
		Statement queryStmt = null;
		PreparedStatement updateStmt = null;
	    try
	    {
	    	Class.forName("com.mysql.jdbc.Driver").newInstance();
	    	String url = "jdbc:mysql://10.50.106.205/recommendationengine_stat_fuzhou";
	    	conn = DriverManager.getConnection(url, "root", "mysql");
	    	queryStmt = conn.createStatement();
	    	rs = queryStmt.executeQuery(queryString);
	    	String userid = null;
	    	String group = null;
	    	int i = 0;
	    	while (rs.next()) {
	    		userid = rs.getString(1);
	    		group = HashABGroupByUserId(userid);
	    		updateStmt = conn.prepareStatement(updateString);
	    		updateStmt.setString(1, group);
	    		updateStmt.setString(2, userid);
	    		updateStmt.executeUpdate();
	    		if (++i % 1000 == 0) {
	    			System.out.println("Processed count: " + i);
	    		}
	    		updateStmt.close();
	    	}
	    }
	    catch (ClassNotFoundException ex) {
	    	System.err.println(ex.getMessage());
    	}
	    catch (IllegalAccessException ex) {
	    	System.err.println(ex.getMessage());
    	}
	    catch (InstantiationException ex) {
	    	System.err.println(ex.getMessage());
    	}
	    catch (SQLException ex) {
	    	System.err.println(ex.getMessage());
    	}
	    catch (NoSuchAlgorithmException e) {
	    	System.err.println(e);
	    }
	    catch (UnsupportedEncodingException e) {
	    	System.err.println(e);
	    }
	    finally {
	    	try {
	    		rs.close();
	    		queryStmt.close();
	    		updateStmt.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	    }

	}
	
	@Test
	public void testHasher() throws NoSuchAlgorithmException, UnsupportedEncodingException {
		for (int i = 0; i < useridArray.length; i++) {
			System.out.println(useridArray[i] + " : " + HashABGroupByUserId(useridArray[i]));
		}
	} 

	
	private String HashABGroupByUserId(String userId) throws NoSuchAlgorithmException, UnsupportedEncodingException
    {
		MessageDigest md5Digest  = MessageDigest.getInstance("MD5");
       String group = "A";
      
       byte[] md5hash;
       synchronized (md5Digest)
       {
           md5hash = md5Digest.digest(userId.getBytes("UTF-8"));
           md5Digest.reset();
       }
      
       long hash = 0L;
       for (int i = 0; i < 8; i++)
       {
           hash = hash << 8 | md5hash[i] & 0x00000000000000FFL;
       }
   
       long round = Math.abs(hash % 2L);
       if(round > 0)
       {
           group = "B";
       }
      
       return group;
    }
	
	private String[] useridArray = {"059093000058","059093000076","059091000288","059093000006","059093000009","059093000012"};
	private String[] testArray =
	 {"02133882765",
		"02133925303",
		"02139850273",
		"02155237990",
		"02157633620",
		"02163302322",
		"02164046553",
		"02165042714",
		"02165350276",
		"02165400925",
		"02165928011",
		"02166108697",
		"02169003703",
		"13391112821",
		"13391237965",
		"18917506209",
		"18917553227",
		"18939944221",
		"48005779",
		"48016537",
		"48033356",
		"48038904",
		"48052875",
		"48055514",
		"48065589",
		"48082830",
		"48104019",
		"48170393",
		"48324312",
		"48428580",
		"48452141",
		"48750395",
		"48774805",
		"48864119",
		"48878448",
		"48898008",
		"50072447",
		"50075782",
		"50195314",
		"50235952",
		"50309398",
		"50424191",
		"50453767",
		"50641175",
		"50772426",
		"50830260",
		"50861719",
		"50925233",
		"50942735",
		"50968098"};

}
